package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
//主界面连接界面
public class MainActivity extends AppCompatActivity {
    private EditText edt_user,edt_pwd,edt_clientId;    //用户名，密码，密钥
    private CheckBox check_reme;                        //记住标志
    private Button btn_connect;                         //按钮

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //makeToast("clientID:" + clientId);
        //绑定控件
        initView();
        //按钮单击事件
        Btn_onClick();
        //下次启动时显示内容
        displayinfo();
    }

    //绑定控件
    private void initView() {
        edt_user=findViewById(R.id.edit_user);
        edt_pwd=findViewById(R.id.edit_pwd);
        edt_clientId=findViewById(R.id.edit_clientId);
        check_reme =findViewById(R.id.check_remember);
        btn_connect=findViewById(R.id.btn_connect);
    }
    //按钮单击事件
    @SuppressLint("ApplySharedPref")
    public void Btn_onClick(){
        btn_connect.setOnClickListener(v -> {
            //存储输入内容
            SharedPreferences.Editor editor=getSharedPreferences("myfile",0).edit();
            //开始存入
            editor.putString("name",edt_user.getText().toString());
            editor.putString("pwd",edt_pwd.getText().toString());
            editor.putString("clientId",edt_clientId.getText().toString());
            editor.putBoolean("st", check_reme.isChecked());
            editor.commit();    //提交数据
            //进入设备界面
            if (!edt_user.getText().toString().equals("") && !edt_pwd.getText().toString().equals("")){
                Intent intent=new Intent(MainActivity.this,EquipmentActivity.class);
                startActivity(intent);
                finish();
            }else {             //提示信息
                Toast.makeText(MainActivity.this, "请输入用户名和密码、密钥", Toast.LENGTH_LONG).show();
            }
        });
    }
    //下次启动时显示密码和用户名
    private void displayinfo(){
        //记住用户名和密码
        String myname=getSharedPreferences("myfile",0).getString("name","");
        String mypwd=getSharedPreferences("myfile",0).getString("pwd","");
        String myclientId=getSharedPreferences("myfile",0).getString("clientId","");
        boolean myst=getSharedPreferences("myfile",0).getBoolean("st",false);
        if (myst){  //如果勾选记住密码，则填写用户名和密码。否则反之。
            edt_user.setText(myname);
            edt_pwd.setText(mypwd);
            edt_clientId.setText(myclientId);
            check_reme.setChecked(true);
        }else {
            edt_user.setText("");
            edt_pwd.setText("");
            edt_clientId.setText("");
            check_reme.setChecked(false);
        }
    }
}



